package io.gitee.chearnee.fw.socket;

import io.gitee.chearnee.fw.socket.component.NettyDispatcher;
import io.gitee.chearnee.fw.socket.component.NettyEventListener;
import io.gitee.chearnee.fw.socket.config.NettyProperties;
import io.netty.channel.nio.NioEventLoopGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(NettyProperties.class)
@ConditionalOnBean(NettyEventListener.class)
public class NettyServiceAutoConfiguration {

    @Autowired
    private NettyProperties nettyProperties;
    @Bean
    @ConditionalOnMissingBean(NettyServerBootstrap.class)
    public NettyServerBootstrap nettyServerBootstrap(){
        return  new NettyServerBootstrap();
    }
    @Bean
    @ConditionalOnMissingBean(NettyDispatcher.class)
    public NettyDispatcher nettyDispatcher(){
        return  NettyDispatcher.getInstance();
    }


    @Bean(destroyMethod = "shutdownGracefully")
    public NioEventLoopGroup bossGroup(){
        return new NioEventLoopGroup(nettyProperties.getBossGroupCount());
    }
    @Bean(destroyMethod = "shutdownGracefully")
    public NioEventLoopGroup workGroup(){
        return new NioEventLoopGroup(nettyProperties.getWorkerGroupCount());
    }
}
